// pages/agent-manage/team-set/team-set.js
let base = {

  /**
   * 页面的初始数据
   */
  data: {
    get_done: false,
    cur_project: {},
    st: {},
    settle_to_check: {},
    pay_mode_check: {},
    is_show: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opts) {
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onUpdate: function() {
    this.getSettingOpt(function(){
      this.detail()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  detail: function() {
    this.post("/project/detail").then(data=>{
      if(data && data.code == 0) {
        this.setData({cur_project: data.res, get_done: true, old_cur_project: data.res})
        this.set_settle_to(data.res.info.settle_to)
        this.update_cloud_access_status()
        this.set_pay_mode(data.res.info.pay_mode)
        this.set_refuse_order(data.res.info)
        this.set_order_handler()
      }
    })
  },

  getSettingOpt: function(callback){
    this.post('/project/setting_options').then(data => {
      if(data && data.code == 0) {
        this.setData({'st' : data.res})
        this.set_order_handler()
        if (typeof(callback)=='function') {
          callback.call(this)
        }
      }
    })
  },

  // 弹窗显示
  show_mode: function(e){
    let is_show = {key:e.currentTarget.dataset.key}
    this.setData({
      is_show:is_show
    })
  },
  // 弹窗隐藏
  hide_mode: function(){
    this.setData({is_show:{}})
  },

  set_order_handler: function(){
    let project = this.data.cur_project
    let st = this.data.st

    let order_handler_index = 0
    if(st && st.order_handler && project && project.info) {
      for(let i = 0; i< st.order_handler.options_arr.length; i++) {
        if(st.order_handler.options_arr[i].value == project.info.order_handler) {
          order_handler_index = i
          break
        }
      }
    }
    this.setData({order_handler_index: order_handler_index})
  },

  set_settle_to: function(settle_to){
    this.data.settle_to_check = {}
    if(settle_to.length) {
      for(let i = 0; i< settle_to.length; i++) {
        this.data.settle_to_check[settle_to[i]] = true
      }
    }
    this.setData({'settle_to_check' : this.data.settle_to_check})
  },

  set_pay_mode: function(pay_modes){
    let pay_mode = this.data.st.pay_mode
    for(let t in pay_mode.options) {
      let checked = false
      let sort = ''
      for(let i=0; i<pay_modes.length; i++) {
        if(pay_modes[i]['pay_id'] == t) {
          checked = true
          sort = pay_modes[i]['sort']
        }
      }
      this.data.pay_mode_check[t] = {id: t, name: pay_mode.options[t], checked: checked, sort: sort}
    }
    this.setData({pay_mode_check: this.data.pay_mode_check})
  },

  set_refuse_order: function(info){
    this.setData({
      time_way_index: info.refuse_order,
      time_remark: info.refuse_order_remark,
      start_d: info.refuse_order_start_d,
      start_t: info.refuse_order_start_t,
      end_d: info.refuse_order_end_d,
      end_t: info.refuse_order_end_t
    })
  },

  set_opt: function(e){
    var value = e.detail.value
    let setting_key = e.target.dataset.key
    this.set_opt_post(setting_key, value)
  },
  set_opt_arr: function(e){
    let setting_key = e.target.dataset.key
    var value = this.data.st[setting_key]['options_arr'][e.detail.value].value
    this.set_opt_post(setting_key, value)
  },
  set_opt_post: function(setting_key, setting_value, callback) {
    if(this.data.old_cur_project && this.data.old_cur_project.info && this.data.old_cur_project.info[setting_key] && this.data.old_cur_project.info[setting_key] == setting_value) {
      return false
    }

    this.post('/project/setting_opt', {setting_key: setting_key, value: setting_value}).then(data => {
      if(data && data.code == 0) {
        wx._showAlert("设置成功")

        if(this.data.cur_project && this.data.cur_project.info && this.data.cur_project.info[setting_key]) {
          this.data.cur_project.info[setting_key] = setting_value
          this.setData({cur_project: this.data.cur_project})
        }

        getApp().broadcastUpdate()
        this.detail()
        if(callback) {
          callback(data)
        }
      }
      else {
        wx._showAlert(data.msg)
      }
    })
  }
}

module.exports = base
